<?php
include('../pages/config.php');
session_start();
####----  This file is used for creating new phone numbers in the Metaswitch

$current_user = $_SESSION['myusername'];
$directoryNumber = $_GET['directoryNumber'];
$persistentProfile = $_GET['persistentProfile'];
$subscriberGroup = $_GET['subscriberGroup'];
$callAgentSignalingType = $_GET['callAgentSignalingType'];
$gatewayName = $_GET['accessDevice'];
$accessDeviceName = preg_replace("/OCCAM-/", "", $gatewayName);
$accessLineNumber = $_GET['accessLineNumber'];
$longDistanceCarrier = $_GET['longDistanceCarrier'];
$intraLATACarrier = $_GET['intraLATACarrier'];
$internationalCarrier = $_GET['internationalCarrier'];
$debug = $_GET['debug'];

$client = new SoapClient('sdk/xml/soap/ShServiceTyped.wsdl');

//print_r($resultCode);
//$pullResultCode = array();
        //  Below are each of the items in the returned Results that we possibly would want to change.
        //  We will "Update" the entire subscriber, even if only 1 change is made.  This is done so that multiple
        //  changes can be made and submitted.  May look at revising this in the future.
        $meta_changes_log = NULL;
	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'SequenceNumber'} = '0';  // zero is passed as the sequence number to indicate we are creating a number
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceIndication'} = 'Meta_Subscriber_BaseInformation';  // zero is passed as the sequence number to indicate we are creating a number
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'MetaSwitchName'} = 'Amherst1';
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'Locale'} = 'English (US)';
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'DirectoryNumber'} = $directoryNumber;
        $meta_changes_log['directoryNumber'] = $directoryNumber;
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'PersistentProfile'} = $persistentProfile;
        $meta_changes_log['persistentProfile'] = $persistentProfile;
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'SubscriberGroup'} = $subscriberGroup;
        $meta_changes_log['subscriberGroup'] = $subscriberGroup;
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'NumberStatus'} = 'Normal';
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'CallAgentSignalingType'} = $callAgentSignalingType;
        $meta_changes_log['callAgentSignalingType'] = $callAgentSignalingType;
	if ($callAgentSignalingType == 'Analog Line/T1 CAS') {
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'AccessDevice'}->{'GatewayName'} = $gatewayName;
        	$meta_changes_log['gatewayName'] = $gatewayName;
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'AccessDevice'}->{'AccessDeviceName'} = $accessDeviceName;
        	$meta_changes_log['accessDeviceName'] = $accessDeviceName;
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'AccessLineNumber'} = $accessLineNumber;
        	$meta_changes_log['accessLineNumber'] = $accessLineNumber;
		
	}
	else if ($callAgentSignalingType == 'SIP') {
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'UseDNForIdentification'} = 'True';
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'SIPDomainName'} = 'voip.wi-net.com';
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'SIPAuthenticationRequired'} = 'True';
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'NewSIPPassword'} = substr($directoryNumber, 3);
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'ConfirmNewSIPPassword'} = substr($directoryNumber, 3);
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'NetworkNode'}->{'UseDefault'} = 'False';
        	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'NetworkNode'}->{'Value'} = 'None';
	}        
	//$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'ServiceSuspended'} = $serviceStatus;
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'LongDistanceCarrier'}->{'Value'} = $longDistanceCarrier;
       	$meta_changes_log['longDistanceCarrier'] = $longDistanceCarrier;
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'IntraLATACarrier'}->{'Value'} = $intraLATACarrier;
       	$meta_changes_log['intraLATACarrier'] = $intraLATACarrier;
        $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'InternationalCarrier'}->{'Value'} = $internationalCarrier;
       	$meta_changes_log['internationalCarrier'] = $internationalCarrier;

if ($debug == 'True') {
	echo "Commands being sent<br/>";
	echo "<pre>";
	print_r($pullResultCode);
	echo "</pre>";        
}

// This section creates the parameters to pass to the Meta for an Update
	$create_params = array(
                        	'UserIdentity' => $directoryNumber,
                                'DataReference' => 0,
                                //  $resultCode->{'UserData'} needs to be passed, because Meta wants to see the Sh-Data first
                                'UserData' => $pullResultCode->{'UserData'},
                                'OriginHost' => 'amherst.com?clientVersion=7.0'
                                );

        //  Send the update and put the returned results into $updateResults
        $createResults = $client->ShUpdate($create_params);

        //  If the returned result code is not a 2001 (Success), then output the error returned
        if ($createResults->{'ResultCode'} != 2001) {
                echo "<br/><br/><b>" . $createResults->{'ExtendedResult'}->{'ExtendedResultDetail'} . "</b>";
        }
        else {
		echo "<b>Added $directoryNumber Successfully!!</b><br/>";
                //  Since the result came back successful, add a log to the database if there were changes made
                //  If a form is just submitted without changes, then don't log anything because no changes were made
                if (!empty($meta_changes_log)) {
                        $meta_changes_csv = NULL;
                        // Parse out the array into a CSV format to be put in the database for records
                        while ($item = current($meta_changes_log)) {
                                $meta_changes_csv .= key($meta_changes_log) . "=" . $item . ",\n";
                                next($meta_changes_log);
                        }
                        mysql_query("INSERT INTO metaswitch_log (trans_id, trans_dn, trans_changes, submitted_by) VALUES (NULL, '$directoryNumber', '$meta_changes_csv', '$current_user')");
                }
        }


if ($debug == 'True') {
	echo "Returned results<br/>";
	echo "<pre>";
	print_r($createResults);
	echo "</pre>";
}

?>

